package cn.dglydrpy.study.j2ee.mq.rabbitmq.producer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
 * 发布订阅模式（fanout）: 
 * 订阅的同一fanout交换器的消费者均进行全量消息消费
 * 若某一消费者不在线，则该消费者收不到消息
 * @author LY_飘涯
 *
 */
public class FanoutProducer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri("amqp://admin:147258aaaaa@192.168.80.131:5672/%2f");

        final Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();
        // 声明fanout类型的交换器
        channel.exchangeDeclare("ex.fanout", "fanout", true, false, null);

        for (int i = 0; i < 20; i++) {
            channel.basicPublish("ex.fanout",
                    "",  // fanout类型的交换器不需要指定路由键
                    null,
                    ("hello world fan:" + i).getBytes("utf-8"));
        }

        channel.close();
        connection.close();
    }
}
